package com.mud.mudcommune.view.fragment.twoLevel;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mud.mudcommune.R;
import com.mud.mudcommune.adapter.CaseAdapter;
import com.mud.mudcommune.entry.CaseContentResponse;
import com.mud.mudcommune.entry.CaseEntry;
import com.mud.mudcommune.http.ApiService;
import com.mud.mudcommune.http.NetUtils;
import com.mud.mudcommune.view.fragment.threeLevel.HotRecommendDetailsFragment;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HotRecommendFragment extends Fragment implements Callback<CaseContentResponse> {
    private static final String TAG = HotRecommendFragment.class.getSimpleName();
    private static final HotRecommendFragment mHotRecommendFragment = new HotRecommendFragment();
    @BindView(R.id.hot_list)
    GridView hotListView;
    @BindView(R.id.head_title)
    TextView title;
    @BindView(R.id.case_back)
    ImageView back;
    Unbinder unbinder;
    @BindView(R.id.loading_layout)
    LinearLayout layout;
    private CaseAdapter mCaseAdapter;
    private List<CaseEntry> caseContentList;

    public static Fragment getInstance() {
        return mHotRecommendFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_hot_recommend, container, false);
        unbinder = ButterKnife.bind(this, view);
        title.setText("热门推荐");
        requestDate();
        hotListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (caseContentList != null) {
                    Fragment detailsFragment = HotRecommendDetailsFragment.getInstance();
                    Bundle bundle = new Bundle();
                    bundle.putInt("caseId", caseContentList.get(position).getCaseId());
                    detailsFragment.setArguments(bundle);
                    getFragmentManager().beginTransaction()
                            .hide(HotRecommendFragment.getInstance())
                            .add(R.id.main_frame, detailsFragment)
                            .addToBackStack(null)
                            .commit();
                }
            }
        });
        return view;
    }

    @OnClick({R.id.case_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.case_back:
                getFragmentManager().popBackStack();
                break;
        }
    }

    // 请求数据
    private void requestDate() {
        NetUtils.createService(ApiService.class,ApiService.BASE_URL).getRecommend().enqueue(this);
    }

    @Override
    public void onResponse(Call<CaseContentResponse> call, Response<CaseContentResponse> response) {
        layout.setVisibility(View.GONE);
        caseContentList = response.body().getCaseContentList();
        if (caseContentList != null && caseContentList.size() > 0) {
            mCaseAdapter = new CaseAdapter(getContext(), caseContentList);
            hotListView.setAdapter(mCaseAdapter);
        }
    }

    @Override
    public void onFailure(Call<CaseContentResponse> call, Throwable t) {
        t.printStackTrace();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
       // unbinder.unbind();
    }
}
